using MultiAgentsShared;
using static MultiAgentsClient.Manager.WorkflowManager;
using static MultiAgentsClient.Presentation.WorkflowListModel;

namespace MultiAgentsClient.Presentation;

public partial record ShareWorkflowPopupModel
{
    private readonly IEventBus eventBus;

    public IState<string> WorkflowJsonInput => State.Value(this, () => "");
    public IState<bool> IsPopupOpen => State.Value(this, () => false);

    public ShareWorkflowPopupModel()
    {
        eventBus = EventBus.Instance;

        eventBus.Subscribe<ShareWorkflowClickCommand>(async cmd => { await ShareWorkflowClickCommandHandler(); });
    }

    public async Task ShareWorkflowClickCommandHandler()
    {
        await IsPopupOpen.SetAsync(true);
    }

    public async ValueTask Close()
    {
        await IsPopupOpen.SetAsync(false);
    }

    public async Task LoadWorkflowByJson()
    {
        string? workflowJson = await WorkflowJsonInput;
        if (string.IsNullOrWhiteSpace(workflowJson))
        {
            return;
        }

        eventBus.Publish(new GetWorkflowCommand(workflowJson));
        await IsPopupOpen.SetAsync(false);
    }
}
